#include "Button.h"


Button::Button()
{
	buttonCounter = Counter<unsigned char>(0, 0);
	pressed = 0;
	held = 0;
	unpressedValue = 0;
	button = BitPointer();
}

Button::Button(unsigned char min, BitPointer v, unsigned char unpressed)
{
	buttonCounter = Counter<unsigned char>(0, min);
	button = v;
	pressed = false;
	held = false;
	unpressedValue = unpressed;
}
Button::Button(Counter<unsigned char> c, BitPointer v, unsigned char unpressed)
{
	buttonCounter = c;
	button = v;
	pressed = false;
	held = false;
	unpressedValue = unpressed;
}
		
void Button::reset()
{
	pressed = false;
	held = false;
	buttonCounter.resetCount(0);
}

unsigned char Button::isHeld()
{
	return held;
}

unsigned char Button::isPressed()
{
	return pressed;
}

void Button::run()
{

	//checks to see if the button is being held
	if(button.getBit()==unpressedValue)
		held = true;
	else
		held = false;

	//increments the count to see if button has been held long enough
	//to be considered pressed.
	if(held == 1)
		buttonCounter++;
	else
		buttonCounter.resetCount(0);

	//if it has been held long enough set press. Be sure to NOT reset it here
	//The reset function is for that!
	if(buttonCounter.flag()==1)
		pressed = true;
}


